/**
 * 用户认证和权限管理相关类型定义
 */

import type {BaseEntity, OperationResult, PageResponse} from '../common/base'

/**
 * 用户类型
 */
export enum UserType {
  SUPER_ADMIN = 'SUPER_ADMIN',
  ADMIN = 'ADMIN',
  DEVELOPER = 'DEVELOPER',
  VIEWER = 'VIEWER'
}

/**
 * 用户状态
 */
export enum UserStatus {
  ENABLED = 'ENABLED',
  DISABLED = 'DISABLED',
  LOCKED = 'LOCKED'
}

/**
 * 产品级权限类型
 */
export enum PermissionType {
  OWNER = 'OWNER',           // 产品负责人 - 完全控制权限
  COLLABORATOR = 'COLLABORATOR',  // 产品协作员 - 读写权限
  REPORTER = 'REPORTER'      // 产品报告员 - 只读权限
}

/**
 * 用户信息
 */
export interface User extends BaseEntity {
  username: string
  displayName: string
  email?: string
  userType: UserType
  status: UserStatus
  lastLoginTime?: string
  remark?: string
}

/**
 * 登录请求
 */
export interface LoginRequest {
  username: string
  password: string
  rememberMe?: boolean
}

/**
 * 登录响应
 */
export interface LoginResponse {
  accessToken: string
  refreshToken: string
  tokenType: string
  expiresIn: number
  userInfo: {
    id: number
    username: string
    displayName: string
    email?: string
    userType: string
    status: string
    lastLoginTime?: string
  }
}

/**
 * 创建用户请求
 */
export interface CreateUserRequest {
  username: string
  password: string
  displayName: string
  email?: string
  userType: UserType
  remark?: string
}

/**
 * 更新用户请求
 */
export interface UpdateUserRequest {
  displayName?: string
  email?: string
  userType?: UserType
  status?: UserStatus
  remark?: string
}

/**
 * 用户查询参数
 */
export interface UserSearchParams {
  username?: string
  displayName?: string
  userType?: UserType
  status?: UserStatus
  page?: number
  size?: number
  sort?: string
  direction?: 'asc' | 'desc'
}

/**
 * 用户分页响应
 */
export type UserPageResponse = PageResponse<User>

/**
 * 产品权限
 */
export interface UserProductPermission extends BaseEntity {
  user: User
  product: {
    id: number
    productCode: string
    productName: string
  }
  permissionType: PermissionType
  grantedBy: User
  grantTime?: string
  remark?: string
}

/**
 * 权限查询参数
 */
export interface PermissionSearchParams {
  userId?: number
  productCode?: string
  permissionType?: PermissionType
  page?: number
  size?: number
}

/**
 * 权限分页响应
 */
export type PermissionPageResponse = PageResponse<UserProductPermission>

/**
 * 操作日志
 */
export interface OperationLog extends BaseEntity {
  user?: User
  operationType: string
  operationDesc: string
  objectType?: string
  objectId?: number
  ipAddress?: string
  userAgent?: string
  operationResult: OperationResult
  errorMessage?: string
}

/**
 * 权限统计
 */
export interface PermissionStats {
  totalUsers: number
  enabledUsers: number
  disabledUsers: number
  totalPermissions: number
  ownerPermissions: number
  collaboratorPermissions: number
  reporterPermissions: number
}